package com.xuecheng.media.controller;

import com.xuecheng.commons.model.dto.*;
import com.xuecheng.commons.model.vo.MediaVo;
import com.xuecheng.commons.model.vo.ResponseResult;
import com.xuecheng.commons.model.vo.VodUploadVo;
import com.xuecheng.commons.utils.AuthInfoHolder;
import com.xuecheng.commons.utils.BeanHelper;
import com.xuecheng.media.domain.Media;
import org.springframework.web.bind.annotation.*;
import com.xuecheng.media.service.MediaService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * <p>
 * 媒资信息前端控制器
 * </p>
 *
 * @author itheima
 */
@Slf4j
@RestController
public class MediaController {

    @Autowired
    private MediaService mediaService;

    // 获取上传凭证
    @PostMapping("/media/vod-token")
    public ResponseResult vodToken(@RequestBody VodUploadDto dto) throws Exception {
        return mediaService.vodToken(dto);
    }

    // 刷新上传凭证
    @GetMapping("/media/refresh-vod-token/{videoId}")
    public ResponseResult refreshToken(@PathVariable("videoId") String videoId) throws Exception {
        return mediaService.refreshToken(videoId);
    }

    // 保存媒资
    @PostMapping("/media")
    public ResponseResult saveMedia(@RequestBody MediaDto dto) throws Exception {
        return mediaService.saveMedia(dto);
    }

    // 媒资分页查询
    @PostMapping("/media/list")
    public ResponseResult findByPage(PageRequest params, @RequestBody MediaQueryDto dto) {
        return mediaService.findByPage(params, dto);
    }

    // 媒资预览
    @GetMapping("/media/preview/{mediaId}")
    public ResponseResult preview(@PathVariable("mediaId") Long mediaId) throws Exception {
        return mediaService.preview(mediaId);
    }

    // 媒资删除
    @DeleteMapping("/media/{mediaId}")
    public ResponseResult deleteMedia(@PathVariable("mediaId") Long mediaId) {
        return mediaService.deleteMedia(mediaId);
    }

    // 媒资审核
    @PutMapping("/media/audit")
    public ResponseResult auditMedia(@RequestBody MediaAuditDto dto) {
        return mediaService.auditMedia(dto);
    }

    // 根据id查询媒资
    @GetMapping("/media/{mediaId}")
    public ResponseResult findById(@PathVariable("mediaId") Long mediaId) {
        System.err.println(AuthInfoHolder.getAuthInfo());

        // 调用service查询
        Media media = mediaService.getById(mediaId);
        // 封装vo
        MediaVo vo = BeanHelper.copyProperties(media, MediaVo.class);
        // 返回vo
        return ResponseResult.okResult(vo);
    }

    /*
        根据id查媒资播放地址
            请求：
                地址：/media/media-url/{mediaId}
                方式：GET
            响应：
                ResourceVo
     */
    @GetMapping("/media/media-url/{mediaId}")
    public ResponseResult findMedisUrl(@PathVariable("mediaId")Long mediaId) throws Exception {
        return mediaService.findMedisUrl(mediaId);
    }
}
